layui.use(['table', 'admin', 'ax', 'func', 'upload','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var upload = layui.upload;
    var laydate = layui.laydate;

    //年月选择器
    laydate.render({
        elem: '#kjqj'
        , type: 'month'
    });

    /**
     * 门店财务主表管理
     */
    var BtyMdCaiwu = {
        tableId: "btyMdCaiwuTable"
    };

    /**
     * 初始化表格的列
     */
    BtyMdCaiwu.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'btyMdCaiwuId', hide: true, title: ''},
            {field: 'yaoma', width: 170, sort: true, title: '妖码'},
            {field: 'dm', sort: true, title: '门店名'},
            {field: 'kjqj', sort: true, title: '会计月'},
            {align: 'center',templet: '#tplOption', title: '操作',width: 1000}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyMdCaiwu.search = function () {
        var queryData = {};
        queryData['yaoma'] = $("#yaoma").val();
        queryData['kjqj'] = $("#kjqj").val();
        queryData['dm'] = $("#dm").val();

        table.reload(BtyMdCaiwu.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    BtyMdCaiwu.openAddDlg = function () {
        func.open({
            title: '添加门店财务主表',
            content: Feng.ctxPath + '/btyMdCaiwu/add',
            tableId: BtyMdCaiwu.tableId
        });
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BtyMdCaiwu.openEditDlg = function (data) {
        func.open({
            title: '修改门店财务主表',
            content: Feng.ctxPath + '/btyMdCaiwu/edit?btyMdCaiwuId=' + data.btyMdCaiwuId,
            tableId: BtyMdCaiwu.tableId
        });
    };

    /**
     * 点击查看资产负债表
     */
    BtyMdCaiwu.onViewZcfzbItem = function (data) {
        func.open({
            title: '资产负债表',
            content: Feng.ctxPath + '/btyMdCaiwu/viewZcfzb?kjqj=' + data.kjqj+"&yaoma="+data.yaoma+"&dm="+data.dm,
            tableId: BtyMdCaiwu.tableId
        });
    };

    /**
     * 点击查看损益表
     */
    BtyMdCaiwu.onViewLrbItem = function (data) {
        func.open({
            title: '损益表',
            content: Feng.ctxPath + '/btyMdCaiwu/viewLrb?kjqj=' + data.kjqj+"&yaoma="+data.yaoma+"&dm="+data.dm,
            tableId: BtyMdCaiwu.tableId
        });
    };

    /**
     * 点击查看损益表
     */
    BtyMdCaiwu.onViewLrbzgItem = function (data) {
        func.open({
            title: '正规损益表',
            content: Feng.ctxPath + '/btyMdCaiwu/viewLrbzg?kjqj=' + data.kjqj+"&yaoma="+data.yaoma+"&dm="+data.dm,
            tableId: BtyMdCaiwu.tableId
        });
    };

    /**
     * 点击配送费用明细
     */
    BtyMdCaiwu.onViewPsfymxItem = function (data) {
        func.open({
            title: '配送费用明细表',
            content: Feng.ctxPath + '/btyMdSfc/viewPsfymx?kjqj=' + data.kjqj+"&yaoma="+data.yaoma+"&dm="+data.dm,
            tableId: BtyMdCaiwu.tableId
        });
    };

    /**
     * 点击查看收发存表
     */
    BtyMdCaiwu.onViewSfcItem = function (data) {
        func.open({
            title: '收发存表',
            content: Feng.ctxPath + '/btyMdSfc/viewSfc?kjqj=' + data.kjqj+"&yaoma="+data.yaoma+"&dm="+data.dm,
            tableId: BtyMdCaiwu.tableId
        });
    };

    /**
     * 查看门店费用明细
     */
    BtyMdCaiwu.showBtyMdFuzhumxz = function () {
        var index=layer.open({
            type: 2,
            title: false,
            content: Feng.ctxPath + '/btyMdFuzhumxz',
            area: ['97%', '97%'],
            success: function(layero, index) {},
            cancel:function (layero, index) {//关闭按钮时操作

            }
        });
        //layer.full(index)
    };

    /**
     * 导出excel按钮
     */
    BtyMdCaiwu.exportExcel = function () {
        var checkRows = table.checkStatus(BtyMdCaiwu.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyMdCaiwu.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyMdCaiwu/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyMdCaiwu.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("btyMdCaiwuId", data.btyMdCaiwuId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyMdCaiwu.tableId,
        url: Feng.ctxPath + '/btyMdCaiwu/list',
        page: true,
        limit:20,
        height: "full-59",
        cellMinWidth: 100,
        cols: BtyMdCaiwu.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyMdCaiwu.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        BtyMdCaiwu.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyMdCaiwu.exportExcel();
    });

    // 查看门店费用明细
    $('#showBtyMdFuzhumxz').click(function () {
        BtyMdCaiwu.showBtyMdFuzhumxz();
    });

    // 工具条点击事件
    table.on('tool(' + BtyMdCaiwu.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyMdCaiwu.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyMdCaiwu.onDeleteItem(data);
        }else if (layEvent === 'viewZcfzb') {
            BtyMdCaiwu.onViewZcfzbItem(data);
        }else if (layEvent === 'viewLrb') {
            BtyMdCaiwu.onViewLrbItem(data);
        } else if (layEvent === 'viewPsfymx') {
            BtyMdCaiwu.onViewPsfymxItem(data);
        }
        else if (layEvent === 'viewSfc') {
            BtyMdCaiwu.onViewSfcItem(data);
        }
        else if (layEvent === 'viewLrbzg') {
            BtyMdCaiwu.onViewLrbzgItem(data);
        }
    });

    /**
     * 导入资产负债表
     */
    var loadIndex;
    upload.render({
        elem: '#importZcfzb'
        , url: '/btyMdCaiwu/importZcfzb'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
            layer.msg("loading......正在导入资产负债表数据，请稍等！");
        }
        , done: function (data) {
            if (data.code == 200) {
                layer.msg("导入资产负债表成功！", {icon: 1, time: 5000}, function () {
                    layer.close(loadIndex);
                    window.location.reload();
                });
            } else {
                layer.close(loadIndex);
                layer.msg(data.message, {icon: 1, time: 5000, shade: 0.2});
            }


        },
    });

    /**
     * 导入损益表
     */
    upload.render({
        elem: '#importSyb'
        , url: '/btyMdCaiwu/importSyb'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
            layer.msg("loading......正在导入损益表数据，请稍等！");
        }
        , done: function (data) {
            if (data.code == 200) {
                layer.msg("导入损益表成功！", {icon: 1, time: 5000}, function () {
                    layer.close(loadIndex);
                    window.location.reload();
                });
            } else {
                layer.close(loadIndex);
                layer.msg(data.message, {icon: 1, time: 5000, shade: 0.2});
            }

        },
    });

    /**
     * 导入配送费用明细表
     */
    upload.render({
        elem: '#importPsfymx'
        , url: '/btyMdSfc/importPsfymx'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
            layer.msg("loading......正在导入配送费用明细数据，请稍等！");
        }
        , done: function (data) {
            if (data.code == 200) {
                layer.msg("导入配送费用明细表成功！", {icon: 1, time: 5000}, function () {
                    layer.close(loadIndex);
                    window.location.reload();
                });
            } else {
                layer.close(loadIndex);
                layer.msg(data.message, {icon: 1, time: 5000, shade: 0.2});
            }

        },
    });

    /**
     * 导入收发存表
     */
    upload.render({
        elem: '#importSfc'
        // , url: '/btyMdSfc/importSfc'
        , url: '/btyMdSfc/addUsers'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
            layer.msg("loading......正在导入收发存数据，请稍等！");
        }
        , done: function (data) {
            if (data.code == 200) {
                layer.msg("收发存正在导入中，请15分钟后刷新此页面查看，期间不建议导入其他表。", {icon: 1, time: 5000}, function () {
                    layer.close(loadIndex);
                    // window.location.reload();
                });
            } else {
                layer.close(loadIndex);
                layer.msg(data.message, {icon: 1, time: 5000, shade: 0.2});
            }

        },
    });


    /**
     * 导入损益表
     */
    upload.render({
        elem: '#importSybzg'
        , url: '/btyMdCaiwu/importSybzg'
        , accept: "file"
        , async: false
        , exts: 'xls|xlsx|xlsm|xlt|xltx|xltm'
        , before: function () {
            loadIndex = layer.load(2, {shade: [0.15, '#ccc']});
            layer.msg("loading......正在导入正规损益表数据，请稍等！");
        }
        , done: function (data) {
            if (data.code == 200) {
                layer.msg("导入正规损益表成功！", {icon: 1, time: 5000}, function () {
                    layer.close(loadIndex);
                    window.location.reload();
                });
            } else {
                layer.close(loadIndex);
                layer.msg(data.message, {icon: 1, time: 5000, shade: 0.2});
            }

        },
    });
});
